var utils = {};
// 时间格式化，如果没有参数则返回当前日期
utils.NewDate = (p) => {
	var d = new Date(p ? p : new Date()),
		month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1,
		day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
	return `${d.getFullYear()}-${month}-${day} ${d.getHours() < 10 ? '0' + d.getHours() : d.getHours()}:${d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()}:${d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds()}`
};
// 时间格式化 - 年月日，如果没有参数则返回当前日期
utils.NewDateYear = (p) => {
	var d = new Date(p ? p : new Date()),
		month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1,
		day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
	return `${d.getFullYear()}-${month}-${day}`
};
// 时间格式化 - 年月日，如果没有参数则返回当前日期
utils.NewDateMonth = (p) => {
	var d = new Date(p ? p : new Date()),
		month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1;
	return `${d.getFullYear()}-${month}`
};
// 返回传入月份前10个月（条形统计图坐标使用）
utils.GetMonthNum = (p) => {
	var d = new Date(p);
	var result = [];
	for (var i = 1; i < 10; i++) {
		d.setMonth(d.getMonth() - 1);
		var m = d.getMonth() + 1;
		m = m < 10 ? "0" + m : m;
		//在这里可以自定义输出的日期格式
		result.push(d.getFullYear() + "-" + m);
		// result.push(d.getFullYear() + "年" + m + "月");
	}
	return result;
}
// 统计分析模块匹配分销商
utils.fenList = (arr) => {
	var installList = [
	  { traderaccount: "atm", oldprice: 0 },
	  { traderaccount: "hn96520", oldprice: 0 },
	  { traderaccount: "wechat", oldprice: 0 },
	  { traderaccount: "wechathn96520", oldprice: 0 },
	  { traderaccount: "Alipay", oldprice: 0 },
	  { traderaccount: "chebada", oldprice: 0 },
	  { traderaccount: "ctrip", oldprice: 0 },
	  { traderaccount: "fliggy", oldprice: 0 },
	  { traderaccount: "Ministry", oldprice: 0 },
	  { traderaccount: "TongCheng", oldprice: 0 },
	];
	installList.forEach((e, i) => {
	  arr.forEach((y, x) => {
		if (e.traderaccount == y.traderaccount) {
		  installList.splice(i, 1, y);
		}
	  });
	});
	return installList;
  }
//给定的日期是第本年中的第几天
function getDayIndexInYear(t) {
	var y, m, d;
	var total = 0;
	var arr = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	y = t.getFullYear()
	m = t.getMonth() + 1
	d = t.getDate()
	for (var i = 0; i < m - 1; i++) {
		total = total + arr[i];
	}
	if ((y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) && m > 2) {
		total = total + d + 1

	} else {
		total = total + d
	}
	return total;
}
// 随机颜色（十六进制）
utils.Color = () => {
	var r = Math.floor(Math.random() * 256);
	var g = Math.floor(Math.random() * 256);
	var b = Math.floor(Math.random() * 256);
	var color = '#' + (Array(6).join(0) + (r.toString(16) + g.toString(16) + b.toString(16))).slice(-6);
	return color;
},
	// 获取当天是今年的第几周（周一到周日为一周）
	// 如果当年1月1日不是周一，则1月第一个周一为第二周的开始
	utils.getWeekInYear = (date) => {
		var endDate = new Date(date),
			curYear = endDate.getFullYear(),
			startDate = new Date(curYear, 0, 1);

		var startWeek = startDate.getDay(), // 1月1号是星期几:0-6
			offsetWeek = 0; //用来计算不完整的第一周，如果1月1号为星期一则为0，否则为1

		if (startWeek != 1) {
			offsetWeek = 1;
			if (!startWeek) {
				startDate.setDate(1);
			} else {
				startDate.setDate(8 - startWeek); // (7 - startWeek + 1)
			}

		}
		var distanceTimestamp = endDate - startDate,
			days = Math.ceil(distanceTimestamp / (24 * 60 * 60 * 1000)) + startWeek,
			weeks = Math.ceil(days / 7) + offsetWeek;
		return weeks;
	}
// 数组对象去重
utils.arrUnique = (arr) => {
	var obj = {};
	arr = arr.reduce((current, next) => {
		obj[next.value] ? "" : obj[next.value] = true && current.push(next);
		return current;
	}, []) //设置current默认类型为数组，并且初始值为空的数组
	return arr
}

// 订单状态判断  非退款订单状态
utils.getStatus = (val) => {
	switch (val) {
		case "0":
			return "待支付";
			break;
		case "1":
			return "已支付";
			break;
		case "2":
			return "已取消";
			break;
		case "3":
			return "已打印";
			break;
		case "6":
			return "申请退款";
			break;
		case "7":
			return "退款成功";
			break;
		case "8":
			return "用户申请退款";
			break;
		case "9":
			return "用户申请退款成功";
			break;

		default:
			break;
	}
};
// 手续费比例不满20取0.5，超过20按1元
utils.calcShou = (m, s) => {
	var shou = (Number(m) * Number(s)) / 100;
	var money1 = Number(m);
	var money2;
	var money3;
	var pay;
	var shouPay;
	// 手续费比例不满20取0.5
	if (s < 20) {
		if (String(shou).indexOf(".") >= 0) {
			money2 = String(shou).split(".", 2);
			// 退款总额
			if (money2[1].substring(0, 1) > 5) {
				money3 = Number(`${Number(money2[0]) + 1}.0`);
				shouPay = money3;
				pay = money1 - shouPay;
			} else {
				money3 = `${money2[0]}.5`;
				shouPay = Number(money3);
				pay = money1 - shouPay;
			}
		} else {
			pay = money1 - shou;
			shouPay = shou;
		}
	} else {
		// 超过20按1元
		shouPay = Math.ceil(shou);
		pay = money1 - shouPay;
	}
	// pay:退款金额，shouPay: 手续费
	return [pay, shouPay];
};

export default {
	utils
}